package com.quanyan.club.web;

import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.mapper.TbClubMemberMapper;
import com.quanyan.comment.serviceFacade.RoleServiceFacade;
import com.quanyan.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * Created by zhuo.shi on 2016/5/30.
 */
@RestController
public class BaseController {

//    @Autowired
    private RoleServiceFacade roleServiceFacade;         //rpc调用

    @Autowired
    private TbClubMemberMapper clubMemberMapper;



    /**
     * 判断是否对某个url有操作权限
     *
     * @param uid     用户ID
     * @param clubId  俱乐部ID
     * @param request Http请求
     * @return 是否有授权
     */
    public Boolean isAuthorize(@NotNull Integer uid, @NotNull Integer clubId, HttpServletRequest request) {
//        TbClubMemberExample tbClubMemberExample = new TbClubMemberExample();
//        tbClubMemberExample.createCriteria().
//                andUidEqualTo(uid).
//                andClubIdEqualTo(clubId).
//                andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
//        List<TbClubMember> tbClubMemberList = clubMemberMapper.selectByExample(tbClubMemberExample);
//        if (tbClubMemberList == null || tbClubMemberList.size() == 0) {
//            return false;
//        }
//        ReqRoleParams reqRoleParams = new ReqRoleParams();
//        reqRoleParams.setRoleCode(tbClubMemberList.get(0).getRoleCode());  //角色编码tb_club_member.role_code
//        reqRoleParams.setUrl(request.getRequestURI());   //资源路径
//        APIResponse<Boolean> authResp = roleServiceFacade.isAuthorityByRole(reqRoleParams);
//        if(!authResp.isRet()){
//            return false;
//        }
//        return authResp.getData();
        return true;
    }

    public int getUid(HttpServletRequest request, boolean isNeedLogin){
        Object uidObj = request.getAttribute("uid");
        int uid = -1;
        if (uidObj != null){
            uid = Integer.valueOf(String.valueOf(uidObj));
        }
        if (uid == -1 && isNeedLogin){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return uid;
    }


}
